﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx;
using System.Windows.Forms;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Profiles;
using Styx.WoWInternals;

namespace RoutesAutomator
{
    public class Bot:IDisposable
    {
        static bool _botIsChanging;

        #region Constructor and Dispose
        public void Dispose()
        {
            _botIsChanging = false;
        }
        #endregion
        /// <summary>
        /// Tells honorbuddy to switch to the specified bot.
        /// </summary>
        /// <param name="name"></param>
        static public void ChangeBot(string name)
        {
            if (_botIsChanging)
            {
                //Professionbuddy.Log("Must wait for previous ChangeBot to finish before calling ChangeBot again.");
                return;
            }
            BotBase bot = BotManager.Instance.Bots.FirstOrDefault(b => b.Key.Contains(name)).Value;
            if (BotManager.Current == bot)
                return;

            if (bot != null)
            {
                // execute from GUI thread since this thread will get aborted when switching bot
                _botIsChanging = true;
                BotManager.Instance.SetCurrent(bot);
                _botIsChanging = false;
            }
            else
            {
                MessageBox.Show("There was a problem setting the bot.");
                //Professionbuddy.Err("Bot {0} does not exist", name);
            }
        }

        /// <summary>
        /// Tells honorbuddy to start botting.
        /// </summary>
        static public void Start()
        {
            IsRunning = true;
        }
        /// <summary>
        /// Tells honorbuddy to stop button.
        /// </summary>
        static public void Stop()
        {
            IsRunning = false;
        }
        /// <summary>
        /// Returns true if honorbuddy is running the bot.
        /// </summary>
        static public bool IsRunning
        {
            get { return TreeRoot.IsRunning; }
            private set 
            {
                if (!TreeRoot.IsRunning && value)
                    TreeRoot.Start();
                else if (TreeRoot.IsRunning && !value)
                    TreeRoot.Stop();

            }
        }
        /// <summary>
        /// Tells honorbuddy to load the specified profile.
        /// </summary>
        /// <param name="pathProfileFileName"></param>
        static public void LoadProfile(string pathProfileFileName)
        {
            if (!string.IsNullOrEmpty(pathProfileFileName))
            {
                ProfileManager.LoadNew(pathProfileFileName);
                return;
            }
            
            if (ProfileManager.CurrentProfile == null)
            {   
                    ProfileManager.LoadEmpty();
            }
        }

        /// <summary>
        /// Returns the current contentent id the bot is currently located.
        /// </summary>
        static public uint CurrentContinendId
        {
            get { return ObjectManager.Me.MapId; }
        }
    }

}
